package com.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.mongo.ShortUrlVoMongoQuery;
import com.service.EncripService;
import com.util.ShortUrlGenerator;
import com.vo.ShortUrlVo;

@Service
public class EncripServiceImpl implements EncripService{

	@Autowired
	ShortUrlVoMongoQuery query;
	
	@Value("${urlHead}")
	private String urlHead;
	
	public String encrip(String url, String key) {
		String shortUrl=ShortUrlGenerator.shortUrl(url)[0];
		ShortUrlVo shortUrlVo=new ShortUrlVo();
		if(key!=null&&!key.equals("")){
			shortUrlVo.setKey(key);
		}
		shortUrlVo.setUrl(url);
		shortUrlVo.setShortUrl(urlHead+shortUrl);
		query.upSert(shortUrlVo, "url");
		return urlHead+shortUrl;
	}

	public String findUrl(String url,String collectionName) {
		return query.findByUrl(url, collectionName).getUrl();
	}

	
}
